package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.entity.PersonInformation;
import com.yaojiaxun.logisticsPlatformSystem.service.PersonInformationService;
import com.yaojiaxun.logisticsPlatformSystem.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2018/10/26 10:42
 * @Description: system后台管理
 */
@Controller
public class SystemController {
    @Autowired
    PersonInformationService personInformationService;

    //后台登陆入口
    @RequestMapping("/sys_login")
    public String SystemLoginPage(Map<String,Object> map){
        return "system/sys/sys_login";
    }

    //后台登陆成功入口
    @RequestMapping("/sys_index")
    public String SystemIndexPage(@RequestParam(required = false) String account, @RequestParam(required = false) String password, HttpSession session) {
        PersonInformation person = personInformationService.findPersonByPersonAccount(account);
        if (person == null){
            //若未登录，直接进入后台主页则跳转到登陆页面
            return "redirect:/sys_login";
        }
        if (person.getProperty().equals("管理员") && person.getBelongedCompany().equals("平台")){
            if (person.getPassword().equals(MD5Util.inputPassToDbPass(password))){
                //session记录登陆状态
                session.setAttribute("admin",person);
                return "system/sys/sys_index";
            }
            else
                return "system/sys/login_error";
        }
        else{
            return "system/sys/login_error";
        }
    }

    //后台注销
    @RequestMapping("/sys_logout")
    public String SystemLogOut(HttpSession session){
        session.removeAttribute("admin");
        return "redirect:/sys_login";
    }

    //驻平台公司管理人员登录入口
    @RequestMapping("/sys_com_login")
    public String sysComLogin(){
        return "system/sys/sys_com_login";
    }
}
